package com.sand.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sand.entity.MessagedetailsEntity;
import com.sand.entity.UserinfoEntity;
import com.sand.service.IMessageService;
import com.sand.service.IQuestionService;
import com.sand.util.ModelTest;
import com.sand.util.PageModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/message")
public class MessageController {
/**
 * @author <achao1441470436@gmail.com>
 * @since 2021/12/5/005 3:44
 */
    //userService接口
    @Resource(name = "messageService")
    private IMessageService messageService;

    //分页查询聊天
    @RequestMapping(value = "/queryMessageLimit")
    public void queryMessageLimit(int limit, int page, String condition, HttpServletRequest request, HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        PageModel<HashMap<String, Object>> pm = messageService.queryMessageLimit(limit, page, condition);
        ModelTest<List<HashMap<String, Object>>> listData = new ModelTest<List<HashMap<String, Object>>>("OK",pm.getList(),0, pm.getPagecount());
        response.getWriter().write(JSON.toJSON(listData).toString());
    }
    //根据id查询聊天详情
    @RequestMapping(value = "/queryMessageById")
    public ModelAndView queryMessageById(int rid,int showWi){
        ModelAndView mv = new ModelAndView().addObject("messageShow",messageService.queryMessageById(rid));

        switch (showWi){
            case 1://后台展示数据
                mv.setViewName("layui/page/table_message_show");
                break;
            case 2://前台展示数据
                mv.setViewName("");
                break;
        }
        return mv;
    }
    //根据id查询聊天详情【根据聊天id获取所有聊天信息】
    @RequestMapping(value = "/getchatlistmsg")
    @ResponseBody
    public String queryMessageById(@RequestParam("rid") int rid){
        //前端数据json对象
        JSONObject json = new JSONObject();
        //获取数据
        HashMap datamap =  messageService.queryMessageById(rid);
        if(datamap!=null){
            json.put("statecode",0);
            json.put("data",datamap);
            json.put("msg","获取成功");
        }else{
            json.put("statecode",1);
            json.put("msg","获取数据失败");
        }
        return json.toJSONString();
    }
    //删除聊天记录
    @RequestMapping(value = "/delMessagedetails")
    public void delMessagedetails(int rid,HttpServletResponse response)throws Exception{
        boolean bln = messageService.delMessagedetails(rid);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //删除聊天对象
    @RequestMapping(value = "/delMessage")
    public void delMessage(String data,HttpServletResponse response)throws Exception{
        boolean bln = messageService.delMessage(data);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("NO");
        }
    }
    //根据租客id和房东id查询会话id
    @RequestMapping(value = "/queryMessageByCondition")
    public ModelAndView queryMessageByCondition(int userId,int landId){
        return new ModelAndView("renting/pages/onther/chat_news").addObject("msgId",messageService.queryMessageByCondition(userId,landId));
    }

    //lyl===========================
    //获取聊天信息列表
    @RequestMapping("/loadchatlist")
    @ResponseBody
    public String loadChatListMsg(HttpServletRequest request){
        //设置json格式对象
        JSONObject json = new JSONObject();
        //获取登录用户
        HashMap<String,Object> usermap;
        if(request.getSession().getAttribute("drom")!=null){
            usermap = (HashMap<String, Object>) request.getSession().getAttribute("drom");
            List<HashMap<String ,Object>> list = (List<HashMap<String, Object>>) messageService.queryMessageByUser(Integer.parseInt(usermap.get("rid").toString()),Integer.parseInt(usermap.get("loginMethod").toString()));
            if(list!=null){
                json.put("statecode",0);
                json.put("msg","获取成功");
                json.put("data",list);
            }else{
                json.put("statecode",1);
                json.put("msg","无法获取聊天信息");
            }
        }else{
            json.put("statecode",1);
            json.put("msg","您未登录无法获取聊天信息");
        }
        return json.toJSONString();
    }


    //实现添加聊天记录
    @RequestMapping("/addNewChatMsg")
    @ResponseBody
    public String addChatData(@RequestParam("rid")int rid,
                              @RequestParam("msg")String msg,
                              HttpServletRequest request){
        //设置前端传输数据对象
        JSONObject json = new JSONObject();
        //获取登录者信息
        HashMap<String,Object> loginuser =null;
        if(request.getSession().getAttribute("drom")!=null){
            loginuser = (HashMap<String, Object>) request.getSession().getAttribute("drom");
        }
        //设置保存到数据库到对象
        MessagedetailsEntity msgobj = new MessagedetailsEntity();
        if(loginuser!=null){
            msgobj.setMessageId(rid);
            msgobj.setUserId((int)loginuser.get("rid"));
            msgobj.setContent(msg);
            //调用service进行持久化操作
            if(messageService.addMessagedetails(JSON.toJSONString(msgobj))){
                json.put("statecode",0);
                json.put("msg","发送成功");
            }else{
                json.put("statecode",1);
                json.put("msg","发送失败");
            }
        }else{
            json.put("statecode",1);
            json.put("msg","未检测到您到登录");
        }
        return json.toJSONString();
    }



}
